package com.example.hangzhou.welbeing.mytestdemoapp.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.hangzhou.welbeing.mytestdemoapp.R;
import com.example.hangzhou.welbeing.mytestdemoapp.adpater.MyDialogAdapter;
import com.example.hangzhou.welbeing.mytestdemoapp.customView.BToast;
import com.example.hangzhou.welbeing.mytestdemoapp.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;

/**
 * 杭州惟君医疗科技有限公司09
 * Created by 20433 on 2018/5/15
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class MyDialogFragment extends Fragment {
    @Bind(R.id.fragment_my_dialog_recyclerview)
    RecyclerView fragmentMyDialogRecyclerview;
    private Context mcontext;
    private MyDialogAdapter mAdapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mcontext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_dialog, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        LinearLayoutManager    mLayoutManager = new LinearLayoutManager(mcontext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mLayoutManager.setReverseLayout(false);//true 从底部开始填充子视图
        fragmentMyDialogRecyclerview.setLayoutManager(mLayoutManager);
        mAdapter = new MyDialogAdapter();
        fragmentMyDialogRecyclerview.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new MyDialogAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view) {
               int postion =  fragmentMyDialogRecyclerview.getChildAdapterPosition(view);
                MyDialog(postion);
            }
        });
    }
    private int checkedItem = 0; //默认选中的item

    private void MyDialog(int postion) {
        switch (postion){
            case 0:
                SingleChoiceItems();
                break;
            case 1:
                MultiChoiceItems();
                break;
            case 2:
                AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
                final AlertDialog dialog = builder.create();
                View dialogView = View.inflate(mcontext, R.layout.dialog_login, null);
                //设置对话框布局
                dialog.setView(dialogView);
                dialog.show();

                break;
            case 3:
                ToastUtil toast = new ToastUtil(mcontext,R.layout.dialog_toast,"市科技局的覅就收入hi人家hi加热i额hi哦热机oh加热i就日记hi哦觉得如何记日记让人窘境hi觉得然后就当日进入hi哦大家hi基地及");
                toast.show();
                break;
            case 4:
                BToast.showText(mcontext, "简单提示 正确图标 长~ ", Toast.LENGTH_LONG, true);
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
        }
    }

    private void MultiChoiceItems() {
        AlertDialog.Builder builder_01 = new AlertDialog.Builder(mcontext,R.style.dialogstyle);
        builder_01.setTitle("请选择你喜欢的颜色：");
        final String[] colors = {"红色", "橙色", "黄色", "绿色", "蓝色", "靛色", "紫色"};
        final List<String> myColors = new ArrayList<>();
        builder_01.setMultiChoiceItems(colors, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked) {
                    myColors.add(colors[which]);
                } else {
                    myColors.remove(colors[which]);
                }
            }
        });

        //设置正面按钮
        builder_01.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                for (String color : myColors) {
                    Log.e(TAG, "选择的颜色：" + color);
                }
               // dialog.dismiss();
            }
        });
        //设置反面按钮
        builder_01.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myColors.clear();
                //dialog.dismiss();
            }
        });
//      AlertDialog dialog = builder.create();
//        dialog.show();
        builder_01.show();
    }

    private void SingleChoiceItems() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
        builder.setTitle("你现在的居住地是：");
        String[] cities = {"北京", "上海", "广州", "深圳", "杭州", "天津", "成都"};

        builder.setSingleChoiceItems(cities, checkedItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                checkedItem = which;
            }
        });
        //设置正面按钮
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        //设置反面按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
